package com.ludomancy.sos64.components;

import java.awt.Graphics;
import java.awt.Point;
import java.util.Vector;

import com.ludomancy.gamelib.ImageAnimation;
import com.ludomancy.gamelib.ImageStrip;
import com.ludomancy.gamelib.Randomizer;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.gamelib.Sound;
import com.ludomancy.sos64.Component;
import com.ludomancy.sos64.Game;

public class Battery extends Component
{
	private ImageAnimation battery;
	private ImageAnimation sparks;
	public boolean isFull = false;
	
	public Battery(Point pos)
	{
		super(pos.x,pos.y);
		if(battery == null)
		{
			battery = new ImageAnimation(new ImageStrip(ResourceManager.getImage("banks.png"), 23, 17), 6);
			sparks = new ImageAnimation(new ImageStrip(ResourceManager.getImage("spark.png"), 9, 13), 2);
			sparks.looping = true;
		}
		battery.looping = true;
		battery.setSequenceBouncingLoop();
	}
	
	public boolean canMove()
	{
		return isFull;
	}
	
	public void onPlacement()
	{
	}
		
	public Point getSize()
	{
		return new Point(23,17);
	}

	public void paint(Graphics g, int px, int py)
	{
		battery.paintCurrentFrame(g, px, py);
		if(isFull)
		{
			sparks.paintCurrentFrame(g, Randomizer.getRandom(px, px + getSize().x - sparks.strip.frameWidth), Randomizer.getRandom(py, py + getSize().y - sparks.strip.frameHeight));
		}
	}
	
	public void onMoved()
	{
		Generator b = null;
		Vector v = Game.game.getComponents(Generator.class);
		if(!v.isEmpty())
		{
			b = (Generator) v.elementAt(0);
		}
		if(b != null && b.area().contains(getHotspot()))
		{
			Sound.play("electric.wav");
			b.active = true;
			Game.game.components.remove(this);
		}
	}

	public void update()
	{
		battery.update();
		sparks.update();
		if(isFull)
		{
			sparks.play();
			battery.play();
		}
	}
}
